package com.cola.utils.baiduMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cola.utils.HttpUtils;
import com.cola.utils.PropertyUtils;
import com.cola.utils.baiduMap.entity.BaiduMapResult;
import org.apache.log4j.Logger;
import java.util.Map;
/**
 * Created by Max on 2017/8/22.
 * 百度地图工具类
 */
public class BaiduMapUtils {
    private static final Logger logger= Logger.getLogger(BaiduMapUtils.class);

    /**
     * 根据地址获取地理位置信息
     * @param address
     * @return
     */
    public static BaiduMapResult getLocationInfoByAddress(String address){
        StringBuilder postUrl = new StringBuilder(PropertyUtils.getProperty("baidu_map_geocoding_api"));

        postUrl.append("address=").append(address);

        postUrl.append("&output=json");

        postUrl.append("&ak=").append(PropertyUtils.getProperty("baidu_ak"));

        String result = HttpUtils.sendGet(postUrl.toString());

        Map<String,Object> resultMap = JSONObject.parseObject(result);

        if(Integer.parseInt(resultMap.get("status").toString())==0){
            return JSONObject.parseObject(result, BaiduMapResult.class);
        }else{
            return null;
        }
    }

    /**
     * 根据经纬度来获取地址
     */
    public static BaiduMapResult getAddressByLngAndLat(String lng, String lat){
        StringBuilder url = new StringBuilder(PropertyUtils.getProperty("baidu_map_geocoding_api"));

        url.append("location="+lat+","+lng);

        url.append("&output=json");

        url.append("&ak=").append(PropertyUtils.getProperty("baidu_ak"));


        String result = HttpUtils.sendGet(url.toString());

        Map<String,Object> resultMap = JSONObject.parseObject(result);

        if(Integer.parseInt(resultMap.get("status").toString())==0){
            return JSONObject.parseObject(result, BaiduMapResult.class);
        }else{
            return null;
        }
    }

    public static void main(String[] args) {
//        System.out.println(JSON.toJSONString(getLocationInfoByAddress("苏州观前街")));
        System.out.println(JSON.toJSONString(getAddressByLngAndLat("114.7024050216","35.2046703717")));
    }
}
